package com.evil.application.enums;

import com.evil.common.application.enums.filter.FilterSourceTypeEnum;
import com.evil.common.core.enums.BaseEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

@AllArgsConstructor
public enum PrintTemplateTypeEnum {

    /**
     * 模板类型(1:通过Word创建,2:通过系统创建)
     */
    UPLOAD_WORD(1, "通过Word创建"),
    SYSTEM(2, "通过系统创建"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<PrintTemplateTypeEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static PrintTemplateTypeEnum findById(Integer id) {
        return PrintTemplateTypeEnum.isExistById(id).orElseThrow(FilterSourceTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "模板类型参数错误");
    }
}
